<?php

namespace App\Admin\Controllers\Wechat;

use App\Models\Wechat;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ResponseController extends Controller
{
    use HasResourceActions;
    protected $header = '微信公众号--资源管理';

    public function __construct()
    {

    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户详情')
            ->description('Detail')
            ->body($this->detail($id,$content));
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Wechat\Response::class, function (Grid $grid) {

            $grid->model()->where('status', 1)->orderBy('id', 'desc');

            $grid->id('ID');
            $grid->type('类型');
            $grid->title('资源标题');
            $grid->eventkey('事件标识');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Wechat\Response::class, function (Form $form) {

            $form->display('id', 'id');
            $form->text('title', '资源标题');
            $form->display('eventkey', '事件标识');
            $form->textarea('remark', '备注');
            $form->radio('status', '状态')->options([
                '1' => '正常',
                '0' => '隐藏',
            ])->default('1');

            $form->radio('type', '类型')->options([
                'text' => '文本',
                'app' => '应用',
            ]);

            $form->hidden('content', '内容')->setElementName('hidden-content')->setElementClass('hidden-content');
            $form->ignore(['hidden-content']);

            Admin::script($this->script());

            $form->tools(function (Form\Tools $tools){
                $tools->disableView();
            });

            $form->disableViewCheck();

            $form->saving(function (Form $form){

                $form->content = json_encode($form->content);

                if (!$form->model()->exists) {
                    $form->model()->eventkey = uniqid();
                }

            });

        });
    }

    protected function script()
    {
        $app_config = json_encode(Wechat\Response::appConfig());

        return <<<EOT

$('.type').on('ifChecked', function () {
    var type = $(this).val();
    
    $('.field-group').remove();
    $('.type-group').remove();
    
    if (type == 'text') {
        addText();
    } else if (type == 'app') {
        addApp();
    }
});

var content = $('.hidden-content').val();
var app_config = $app_config;

if(content){
    content = JSON.parse(content);
    if(content.content) {
        addText(content.content);
    } else if (content.app){
        addApp(content);
    }
}


function addText(content = ''){
    
    var html = '<div class="form-group type-group"><label for="content" class="col-sm-2  control-label">文本内容<\/label><div class="col-sm-8"><textarea name="content[content]" class="form-control" rows="2" placeholder="输入 文本内容">'+content+'<\/textarea><\/div><\/div>';
    $(".fields-group").append(html);
}

function addApp(content = ''){
    
    var select_html = '<select class="form-control" name="content[app]" id="app_select">';
    var id_html = '';
    for(var item in app_config){
    
        var selected = '';
        if(content && content.app == item){
            var name = app_config[item].name;
            if(app_config[item].config) {
                console.log(app_config[item].config);
                for(var config in app_config[item].config) {
                    var caption = app_config[item].config[config].caption;
                    var field = app_config[item].config[config].field;
                    var id = content[field] ? content[field] : '';

                    id_html += '<div class="form-group field-group"><label for="title" class="col-sm-2  control-label">'+caption+'<\/label><div class="col-sm-8"><div class="input-group"><span class="input-group-addon"><i class="fa fa-pencil fa-fw"><\/i><\/span><input type="text" name="content['+field+']" value="'+id+'" class="form-control"><\/div><\/div><\/div>';
                }
            }
            
            selected = 'selected';
        }
        
        select_html += '<option '+selected+' value="'+item+'">'+app_config[item].name+'<\/option>';
    }
    select_html += '<\/select>';

    var html = '<div class="form-group type-group"><label for="content" class="col-sm-2  control-label">应用<\/label><div class="col-sm-8">'+select_html+'<\/div><\/div>';
    html += id_html;
    $(".fields-group").append(html);
    
}

$(".fields-group").delegate('#app_select', 'change', function(){
    var selected = $('#app_select option:selected').val();
    console.log(selected)
    var config = app_config[selected];
    
    $('.field-group').remove();
    var id_html = '';
    if(app_config[selected] && app_config[selected].config) {
        for(var con in app_config[selected].config) {
            var caption = app_config[selected].config[con].caption;
            var field = app_config[selected].config[con].field;
            var id = content[field] ? content[field] : '';
            
            id_html += '<div class="form-group  field-group"><label for="title" class="col-sm-2  control-label">'+caption+'<\/label><div class="col-sm-8"><div class="input-group"><span class="input-group-addon"><i class="fa fa-pencil fa-fw"><\/i><\/span><input type="text" name="content['+field+']" value="'+id+'" class="form-control"><\/div><\/div><\/div>';
        }
        
        $(".fields-group").append(id_html);
    }
});







EOT;
    }

    public function select(Request $request)
    {
        $response = Wechat\Response::paginate(15);

        return response()->json($response);

        /*return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');
            $content->body($this->grid1());
        });*/
    }

    protected function grid1()
    {
        return Admin::grid(Wechat\Response::class, function (Grid $grid) {

            $grid->model()->where('status', 1)->orderBy('id', 'desc');

            $grid->id('ID');
            $grid->type('类型');
            $grid->title('资源标题');
            $grid->eventkey('事件标识');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

        });
    }

}
